﻿//    This file is part of EliteCollection.
//
//    EliteCollection is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    EliteCollection is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with EliteCollection.  If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;
using System.Text.RegularExpressions;

namespace DatabaseConnector
{
    /// <summary>
    /// EliteCollectionSystem Database Connector Class.
    /// </summary>
    public class DatabaseConnector
    {
        /// <summary>
        /// Connects to a Database and returns a FlatConn.
        /// </summary>
        /// <param name="databaseFile">Path to Database file.</param>
        /// <returns>Flat File Connection.</returns>
        public FlatConn connect(String databaseFile)
        {
            //Check if Database Exists
            if (File.Exists(databaseFile) == false)
            {
                throw new Exception("Error Database Doesnt Exist!");
            }
            //Open File for Reading
            String databaseContents = File.ReadAllText(databaseFile);
            Array tempDatabaseLines = File.ReadAllLines(databaseFile);
            //Create the FlatConn
            FlatConn tempFlatConn = new FlatConn();
            tempFlatConn.databaseContents = databaseContents;
            tempFlatConn.databaseFile = databaseFile;
            //Get Lines
            int line = 0;
            ArrayList tempArray = new ArrayList();
            foreach (string item in tempDatabaseLines)
            {
                tempArray.Add(item);
                line += 1;
            }
            tempFlatConn.databaseLines = tempArray;
            //Get Columns
            ArrayList outputColumns = new ArrayList();
            string[] splitColumns = Regex.Split(tempArray[0].ToString(), "\t");
            foreach (string item in splitColumns)
            {
                if (item != "" && item != null && item != "\t")
                {
                    outputColumns.Add(item);
                }
            }
            tempFlatConn.columns = outputColumns;
            //Get Rows
            ArrayList tempRows = new ArrayList();
            int line2 = 0;
            foreach (string item in tempDatabaseLines)
            {
                if (line2 != 0)
                {
                    string rowitemdata = "";
                    string[] rowitemssplit = Regex.Split(item, "\t");
                    int currrowitem = 0;
                    foreach (string rowitem in rowitemssplit)
                    {
                        if (rowitem != "" && rowitem != null && rowitem != "\t")
                        {
                            if (currrowitem != rowitemssplit.Length - 1)
                            {
                                rowitemdata += rowitem + "<->";
                            }
                            else
                            {
                                rowitemdata += rowitem;
                            }
                        }
                        currrowitem += 1;
                    }
                    tempRows.Add(rowitemdata);
                }
                line2 += 1;
            }
            tempFlatConn.rows = tempRows;
            //Return the FlatConn
            return tempFlatConn;
        }
        /// <summary>
        /// Gets a whole row from the database.
        /// </summary>
        /// <param name="conn">Reference to Flat File Connection.</param>
        /// <param name="rowid">Id of row to get, Starts at 0.</param>
        /// <returns>String of the requested row.</returns>
        public String getRow(FlatConn conn, int rowid)
        {

            return "";
        }
        /// <summary>
        /// Gets an item from a requested row.
        /// </summary>
        /// <param name="conn">Reference to Flat File Connection.</param>
        /// <param name="rowId">Id of row to get, Starts at 0.</param>
        /// <param name="colId">Id of item to get, Starts at 0.</param>
        /// <returns>String of the requested row item.</returns>
        public String getRowItem(FlatConn conn, int rowId, int colId)
        {

            return "";
        }
    }
}
